package com.guit.hrms.config;

import com.guit.hrms.interceptors.AllRequestInterceptor;
import com.guit.hrms.interceptors.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private LoginInterceptor loginInterceptor;
    @Autowired
    private AllRequestInterceptor allRequestInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //登录和上传简历的接口不拦截
        registry.addInterceptor(loginInterceptor)

                .addPathPatterns("/**")  // 拦截所有请求
                .excludePathPatterns(
                        "/api/login",
                        "/api/login/wx",
                        "/api/interview/interviewee/resume/add",
                        "/api/articles",                    // 获取文章列表不需要验证
                        "/api/recruit/announcements/get",
                        "/api/approval/recruit/announcements/get",
                        "/api/interview/interviewee/offer/result"
//                        "/getEmpInfo/personal"
//                    "/articles/**"            // 获取指定ID的文章不需要验证
//                    "/articles/type/*"              // 根据类型获取文章不需要验证
                );


//        registry.addInterceptor(new HandlerInterceptor() {
//            @Override
//            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){
//                MDC.put("traceId", UUID.randomUUID().toString().substring(0,8));
//                return true;
//             }
//        });
        registry.addInterceptor(allRequestInterceptor)

                .addPathPatterns("/**")  // 拦截所有请求
                .excludePathPatterns(
                        "/api/login",
                        "/api/login/wx",
                        "/api/interview/interviewee/resume/add",
                        "/api/articles",                    // 获取文章列表不需要验证
                        "/api/recruit/announcements/get",
                        "/api/approval/recruit/announcements/get",
                        "/api/interview/interviewee/offer/result"

                );

    }


    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:8081", "http://localhost:8082", "http://localhost:3000")  // 明确指定允许的前端源
                .allowedMethods("GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS")
                .allowCredentials(true)  // 允许带凭证的请求
                .allowedHeaders("Token", "Content-Type", "Authorization", "*")  // 允许特定头部
                .exposedHeaders("Set-Cookie");  // 如果你有返回 Cookie 或其他信息
    }

}
